﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using HZ.Supervise.Model;
using HZ.Supervise.IBLL;

namespace HZ.Supervise.Web.Admin
{
    public partial class HospitalUser : BasePage
    {
        IBLL_User iu = BLLFactory.BLL_User();
        KeyOperationLogJG kj;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                ddlArea.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
                //ddlAreaDL.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
                bindUser();
            }
        }
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            bindUser();
        }

        private void bindUser()
        {
            int totalrecord = 0;
            DataTable dt = iu.GetHospital(str(), this.AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalrecord);
            AspNetPager1.RecordCount = totalrecord;
            gvw.PageSize = AspNetPager1.PageSize;
            gvw.DataSource = dt; 
            gvw.DataBind();
        }
        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns>where</returns>
        string str()
        {
            StringBuilder sb = GetArea();
            //sb.AppendFormat("{0}", GetAreaDL());
            //sb.AppendFormat(" and v.AreaID='{0}'", ddlArea.SelectedAreaID);
            //查询用户ID
            if (!string.IsNullOrEmpty(txtUserId.Text.Trim()))
            {
                sb.AppendFormat(@" AND CHARINDEX('{0}',UserID)>0", txtUserId.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                sb.AppendFormat(@" AND CHARINDEX('{0}',UserName)>0", txtUserName.Text.Trim());
            } 
            if (!string.IsNullOrEmpty(ddlIsUsing.SelectedValue))
            {
                sb.AppendFormat(@" AND IsUsing={0}", Convert.ToInt32(ddlIsUsing.SelectedValue));
            }
            if (!string.IsNullOrEmpty(ddlAdmin.SelectedValue))
            {
                sb.AppendFormat(@" AND ISAdmin={0}", Convert.ToInt32(ddlAdmin.SelectedValue));
            }
            if (!string.IsNullOrEmpty(dplLevel.SelectedValue))
            {
                sb.AppendFormat(@" AND Levels1 = '{0}'", dplLevel.SelectedValue);
            }
            User u = iu.GetUserByUserID(CurrentAccount.GetUser().UserID);
            if (u.IsJK==true)
            {
                sb.AppendFormat(@" and v.HospitalID like 'jk%'");
            }
            return sb.ToString();
        }
        private StringBuilder GetArea()
        {
            StringBuilder sb = new StringBuilder();
            string id = CurrentAccount.GetUser().Org.Area.AreaID;
            if (id == "510000")//省级监督机构
            { 
                if (ddlArea.SelectedAreaID == "510000")
                {
                    sb.AppendFormat(@" AND SUBSTRING( v.AreaID,1,2) ='" + ddlArea.SelectedAreaID.Substring(0, 2) + "' ");
                }
                else if (ddlArea.SelectedAreaID.Length==4)//(ddlArea.SelectedAreaID.Substring(4, 2) == "00")
                {
                    sb.AppendFormat(@" AND SUBSTRING( v.AreaID,1,4) ='" + ddlArea.SelectedAreaID.Substring(0, 4) + "' ");
                }
                else
                {
                    sb.AppendFormat(@" AND v.AreaID ='" + ddlArea.SelectedAreaID + "' ");
                }
            }
            else if (id.Substring(4, 2) == "00")  //市级
            { 
                if (ddlArea.SelectedAreaID.Length == 4) 
                {
                    sb.AppendFormat(@" AND SUBSTRING( v.AreaID,1,4) ='" + ddlArea.SelectedAreaID.Substring(0, 4) + "' ");
                }
                else
                {
                    sb.AppendFormat(@" AND v.AreaID ='" + ddlArea.SelectedAreaID + "' ");
                }
            }
            else
            { 
                sb.AppendFormat(@" AND v.AreaID ='" + ddlArea.SelectedAreaID + "' ");
            }
            return sb;
        }
        //private StringBuilder GetAreaDL()
        //{
        //    StringBuilder sb = new StringBuilder();
        //    string id = CurrentAccount.GetUser().Org.Area.AreaID;
        //    if (id == "510000")//省级监督机构
        //    {
        //        if (ddlAreaDL.SelectedAreaID == "510000")
        //        {
        //            sb.AppendFormat(@" AND SUBSTRING( v.AreaIDdl,1,2) ='" + ddlAreaDL.SelectedAreaID.Substring(0, 2) + "' ");
        //        }
        //        else if (ddlAreaDL.SelectedAreaID.Length == 4)//(ddlArea.SelectedAreaID.Substring(4, 2) == "00")
        //        {
        //            sb.AppendFormat(@" AND SUBSTRING( v.AreaIDdl,1,4) ='" + ddlAreaDL.SelectedAreaID.Substring(0, 4) + "' ");
        //        }
        //        else
        //        {
        //            sb.AppendFormat(@" AND v.AreaIDdl ='" + ddlAreaDL.SelectedAreaID + "' ");
        //        }
        //    }
        //    else if (id.Substring(4, 2) == "00")  //市级
        //    {
        //        if (ddlAreaDL.SelectedAreaID.Length == 4)
        //        {
        //            sb.AppendFormat(@" AND SUBSTRING( v.AreaIDdl,1,4) ='" + ddlAreaDL.SelectedAreaID.Substring(0, 4) + "' ");
        //        }
        //        else
        //        {
        //            sb.AppendFormat(@" AND v.AreaIDdl ='" + ddlAreaDL.SelectedAreaID + "' ");
        //        }
        //    }
        //    else
        //    {
        //        sb.AppendFormat(@" AND v.AreaIDdl ='" + ddlAreaDL.SelectedAreaID + "' ");
        //    }
        //    return sb;
        //}
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }
        //初始化密码
        protected void btnBatResetPws_Click(object sender, EventArgs e)
        {
            string strChkUsers = Request["chkUser"];
            string s = "";
            if (!string.IsNullOrEmpty(strChkUsers))
            {
                string[] arryUsers = strChkUsers.Split(new char[',']);
                List<string> userIds = new List<string>();
                for (int i = 0; i < arryUsers.Length; i++)
                {
                    userIds.Add(arryUsers[i]);
                }

                if (iu.InitPassword(userIds, Constants.USER_DEFAULT_PWS))
                {
                    foreach (string u in userIds)
                    {
                        s += u + ",";
                    }
                    s = s.Trim(',');
                    AddLog(string.Format("批量初始化医疗机构[{0}]密码成功!", s));
                    PageInfo("用户" + strChkUsers + "的密码已被初始化为" + Constants.USER_DEFAULT_PWS, "");
                }
                else
                {
                    PageError("密码初始化失败", "");
                }
            }
        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="msg"></param>
        private void AddLog(string msg)
        {
            kj = new KeyOperationLogJG();
            kj.OperationLogSerialID = Guid.NewGuid().ToString();
            kj.Operator = CurrentAccount.GetUser().UserID;
            kj.OrgID = CurrentAccount.GetUser().Org.OrgID;
            kj.OperateTime = DateTime.Now;
            kj.OperateMessage = msg;
            iu.AddKeyOperationLog(kj);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            int totalCount = 0;
            System.Data.DataTable dt = iu.GetHospital(str(), 1, 0, out totalCount); ;
            AddLog(string.Format("导出数据成功!"));
            ExportFile.Instance.ExportData(dt, gvw, this.Page);
        }

    }
}